package com.lt.schoolchat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lt.schoolchat.common.ErrorCode;
import com.lt.schoolchat.exception.BusinessException;
import com.lt.schoolchat.mapper.PostMapper;
import com.lt.schoolchat.model.entity.Post;
import com.lt.schoolchat.model.enums.PostGenderEnum;
import com.lt.schoolchat.model.enums.PostReviewStatusEnum;
import com.lt.schoolchat.service.PostService;
import com.lt.schoolchat.utils.GreenImageScan;
import com.lt.schoolchat.utils.GreenTextScan;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author teng
 * @description 针对表【post(帖子)】的数据库操作Service实现
 */
@Service
@Slf4j
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    @Resource
    private GreenImageScan greenImageScan;
    @Resource
    private GreenTextScan greenTextScan;

    @Override
    public void validPost(Post post, boolean add) {
        if (post == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer age = post.getAge();
        Integer gender = post.getGender();
        String education = post.getEducation();
        String place = post.getPlace();
        String job = post.getJob();
        String contact = post.getContact();
        String loveExp = post.getLoveExp();
        String content = post.getContent();
        String photo = post.getPhoto();
        Integer reviewStatus = post.getReviewStatus();
        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(content, job, place, education, loveExp, photo, contact) || ObjectUtils.anyNull(age, gender)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(content) && content.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }
        if (reviewStatus != null && !PostReviewStatusEnum.getValues().contains(reviewStatus)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (age != null && (age < 18 || age > 100)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "年龄不符合要求");
        }
        if (gender != null && !PostGenderEnum.getValues().contains(gender)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "性别不符合要求");
        }
        if (StringUtils.isNotBlank(contact) && !StringUtils.isNumeric(contact)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "电话号码有误");
        }
//        try {
//            // 阿里云文本审核，将敏感词转换为 *
//            Map textMap = greenTextScan.greenTextScan(content);
//            if (textMap != null) {
//                String suggestion = (String) textMap.get("suggestion");
//                if ("block".equals(suggestion)) {
//                    // 含有违规内容 评论内容替换
//                    content = (String) textMap.get("filteredContent");
//                    post.setContent(content);
//                }
//            }
//            // 阿里云 图片审核
//            Map imageMap = greenImageScan.imageUrlScan(Collections.singletonList(photo));
//            if (imageMap != null) {
//                String suggestion = (String) imageMap.get("suggestion");
//                if ("block".equals(suggestion)) {
//                    // 含有违规内容
    //                    String reason = (String) imageMap.get("reason");
//                    throw new BusinessException(ErrorCode.OPERATION_ERROR, "上传失败，原因：" + reason);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("阿里云内容安全审核异常，原因：{}", e.getMessage());
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
//        }
    }
}




